package com.dalonedrau.jogl.util;
/**
 * 
 */

/**
 * @author Donald
 *
 */
public final class RefIdArray {
	public int length() {
		if (array == null) {
			array = new int[0];
		}
		return array.length;
	}
	/**
	 * Gets the number of ids in the array.
	 * @return <code>int</code>
	 */
	public int getNumInArray() {
		return numInArray;
	}
	/**
	 * Gets a value from the array.
	 * @param index the array-index to be retrieved
	 * @return <code>int</code>
	 * @throws Exception
	 */
	public int get(final int index) throws Exception {
		return array[index];
	}
	/** the array of reference ids. */
	private int[] array;
	/** the counter for the number of ids in the array. */
	private int numInArray;
	/**
	 * Adds an item to an array of reference ids.  After execution, the array 
	 * will contain the reference id to be added.  If the ref id to be added
	 * can be added, then the counter for the number of ids in the array will
	 * be incremented, otherwise that number will stay the same.
	 * @param toAdd the reference id to be added
	 */
	public void addItemToRefIdArray(final int toAdd) {
		if (array == null) {
			array = new int[0];
		}
		boolean found = false;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == toAdd) {
				found = true;
				break;
			}
		}
		if (!found) {
			if (array.length <= numInArray) {
				// extend the array
				int[] dest = new int[numInArray + 1];
				System.arraycopy(array, 0, dest, 0, array.length);
				array = dest;
				dest = null;
			}
			array[numInArray] = toAdd;
			numInArray++;
		}
	}
	/**
	 * Removes an item from an array of reference ids.  After execution, the 
	 * array will not contain the reference id to be removed.  If the ref id to 
	 * be removed can be removed, then the counter for the number of ids in the 
	 * array will be decremented, otherwise that number will stay the same.
	 * @param toRemove the reference id to be removed
	 */
	public void removeItemFromRefIdArray(final int toRemove) {
		if (array == null) {
			array = new int[0];
		}
		boolean found = false;
		int index = 0;
		for (; index < array.length; index++) {
			if (array[index] == toRemove) {
				found = true;
				break;
			}
		}
		if (found) {
			if (index == 0) {
				// index is first index in array
				// move everything back one position in ios array
				int[] dest = new int[array.length];
				System.arraycopy(array, 1, dest, 0, array.length - 1);
				// last item in array is set to -1, which is an invalid ref id
				dest[array.length - 1] = -1;
				array = dest;
				dest = null;
			} else if (index == array.length - 1) {
				// index is last index in array.
				// set last index value to invalid
				array[index] = -1;
			} else {
				// index is somewhere in the middle
				int[] dest = new int[array.length];
				// copy everything leading up to index
				System.arraycopy(array, 0, dest, 0, index);
				// copy everything after index
				System.arraycopy(array, index + 1, dest, index, 
						array.length - 1 - index);
				// last item in array is set to -1, which is an invalid ref id
				dest[array.length - 1] = -1;
				array = dest;
				dest = null;
			}
			numInArray--;
		}
	}
}
